package com.mycompany.ui;

import java.util.Iterator;

import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor;

import com.mycompany.ui.panel.CategoriaPanel;
import com.mycompany.ui.panel.ProdottoPanel;
import com.mycompany.ui.panel.UserLockTestPanel;
import com.mycompany.ui.panel.UserPanel;
import com.mycompany.ui.panel.searches.ClientiSearchPanel;
import com.mycompany.ui.panel.searches.GenericSearchPanel;
import com.mycompany.ui.panel.searches.UserSearchPanel;
import com.vaadin.ui.Accordion;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;

public class AccordionMenu extends Panel {

	private Button buttonClienti = new Button("Clienti");
	private Button buttonProdotti = new Button("Prodotti");
	private Button buttonCategoria = new Button("Categoria");
	private Button buttonProvaTestLock = new Button("ProvaTestLock");
	
	private VerticalLayout layoutAmministrazione = new VerticalLayout();
	
	private Button ricercaClienti = new Button("RicercaClienti");
	private Button ricercaProdotti = new Button("RicercaProdotti");
	private VerticalLayout layoutRicerche = new VerticalLayout();
	
	private Accordion accordion = new Accordion();
	private TabSheet tabSheet;
	
	private ProdottoPanel prodottoPanel = new ProdottoPanel();
	
	public AccordionMenu(TabSheet tabSheet) {
		// TODO Auto-generated constructor stub
		
		this.tabSheet = tabSheet;
		buildLayout();
		addListenerToPanel();
		setCaption("Menu");
		setContent(accordion);
	}

	private void addListenerToPanel() {
		// TODO Auto-generated method stub
		buttonClienti.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				
				Tab tab = isExist("Clienti");
				if(tab != null)
					tabSheet.setSelectedTab(tab);
				else{
					addTabToTabSheet(new UserPanel(), "Clienti");
				}
			}
		});
		
		buttonProdotti.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				
				Tab tab = isExist("Prodotti");
				
				if(tab != null){
					prodottoPanel.fillContainerComboBox();
					tabSheet.setSelectedTab(tab);
				}
				else
					addTabToTabSheet(prodottoPanel, "Prodotti");
					
				
			}
		});
		
		buttonCategoria.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				
				Tab tab = isExist("Categoria");
				if(tab != null){
					
					tabSheet.setSelectedTab(tab);
					
				}
				else
					addTabToTabSheet(new CategoriaPanel(), "Categoria");
					
				
			}
		});
		
		ricercaClienti.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				
				Tab tab = isExist("Ricerca Clienti");
				if(tab != null)
					tabSheet.setSelectedTab(tab);
				else
					addTabToTabSheet(new ClientiSearchPanel(), "Ricerca Clienti");
					
				
			}
		});
		
		ricercaProdotti.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				
				Tab tab = isExist("Ricerca Prodotti");
				if(tab != null)
					tabSheet.setSelectedTab(tab);
				else
					addTabToTabSheet(new UserSearchPanel(), "Ricerca Prodotti");
					
				
			}
		});
		
		buttonProvaTestLock.addClickListener(new ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				Tab tab = isExist("Test Lock User");
				if(tab != null)
					tabSheet.setSelectedTab(tab);
				else
					addTabToTabSheet(new UserLockTestPanel(), "Test Lock User");


			}
		});
		
	}
	
	private void addTabToTabSheet(Component componentToAdd , String caption){
		
		tabSheet.addTab(componentToAdd,caption);
		tabSheet.setSelectedTab(componentToAdd);
	}
	
	//Check if a tab is open
	public Tab isExist(String captionCap){
		
		Iterator<Component> i = tabSheet.getComponentIterator();
		while (i.hasNext()) {
		    Component c = (Component) i.next();
		    Tab genTab = tabSheet.getTab(c);
		    if (captionCap.equals(genTab.getCaption())) {
		         return genTab;
		    }
		}
		
		return null;
	}
	
	private void buildLayout() {
		// TODO Auto-generated method stub
		
		buttonClienti.setWidth("100%");
		buttonProdotti.setWidth("100%");
		buttonCategoria.setWidth("100%");
		ricercaClienti.setWidth("100%");
		ricercaProdotti.setWidth("100%");
		buttonProvaTestLock.setWidth("100%");
		
		layoutAmministrazione.addComponent(buttonClienti);
		layoutAmministrazione.addComponent(buttonProdotti);
		layoutAmministrazione.addComponent(buttonCategoria);
		accordion.addTab(layoutAmministrazione,"AMMINISTRAZIONE",null);
		
		layoutRicerche.addComponent(ricercaClienti);
		layoutRicerche.addComponent(ricercaProdotti);
		layoutRicerche.addComponent(buttonProvaTestLock);
		accordion.addTab(layoutRicerche,"RICERCHE",null);
		
	}
	
	
	
	
}
